
PermanentGameelement = Gameelement:new{
	hp = 100,
	hpMax = 100,
	hpRegenerationRate = 1,
	mana = 100,
	manaRegenerationRate = 1,
	manaMax = 100,
	movementSpeed = 0,
	
	passable = boolean,

	attack = {
		meleRange = 0,
		meleDamage = 0,
		meleSpeed = 0,
		
		distanceDamage = 0,
		distanceSpeed = 0
	},
	skills = {}
	
} 



function PermanentGameelement:getDamage(damage, causer)
	self.hp = self.hp - damage
	self:newoht(damage)
	if self.hp < 1 then self:destroy(causer) end
end

function PermanentGameelement:regenerateHp()
	if 		self.hpRegTime == nil or self.hpRegTime < clock.second then
			self.hpRegTime = clock.second + 1

	elseif	self.hpRegTime == clock.second then
			self:getHp(self.hpRegenerationRate)
			self.hpRegTime = clock.second + 1
	end
	
	if self.hpRegTime > 59 then self.hpRegTime = 0 end
end

function PermanentGameelement:getMana(amount)
	if (mana + amount < manaMax) then
		self.mana = self.mana + amount
	else
		mana = manaMax
	end
end

function PermanentGameelement:getHp(amount)
	if (self.hp + amount < self.hpMax) then
		self.hp = self.hp + amount
	else
		hp = hpMax
	end
end


function PermanentGameelement:regenerateMana()
	if 		self.manaRegTime == nil or self.manaRegTime < clock.second then
			self.manaRegTime = clock.second + 1

	elseif	self.manaRegTime == clock.second then
			self:getMana(self.manaRegenerationRate)
			self.manaRegTime = clock.second + 1
	end
	
	if self.manaRegTime > 59 then self.manaRegTime = 0 end
end

function PermanentGameelement:meleAttack(targets) -- an attck which hits enemies, collected in the table targets, and deal them damage
	if self.dir == 'north' then 		
	  	self.attack.x = self.x 
	  	self.attack.y = self.y - (self.attack.melemeleRange)

	  	elseif self.dir == 'northeast' then
	  	self.attack.x = self.x + (self.wid/2)                                 --TODO: calibrate the final range
	  	self.attack.y = self.y

		  elseif self.dir == 'east' then	
	  	self.attack.x = self.x
	  	self.attack.y = self.y + (self.hig/2)								

	  	elseif self.dir == 'southeast' then
	  	self.attack.x = self.x - (self.attack.meleRange/2)
	  	self.attack.y = self.y						

	 	 elseif self.dir == 'south' then
	  	self.attack.x = self.x + (self.wid/2)
	  	self.attack.y = self.y - (self.attack.meleRange/2)						

		  elseif self.dir == 'southwest' then
	  	self.attack.x = self.x + (self.wid/2)
	  	self.attack.y = self.y + (self.hig/2)						

		 elseif self.dir == 'west' then
	  	self.attack.x = self.x - (self.attack.meleRange/2)
	  	self.attack.y = self.y - (self.hig/2)						

		  elseif self.dir == 'northwest' then
	  	self.attack.x = self.x - (self.attack.meleRange/2)
	  	self.attack.y = self.y - (self.attack.meleRange/2)						
	  end

	  if #target > 0 then
	  	for i = 1, #target do
	  		if 	compare(self.attack.x, self.attack.y, self.attack.wid, self.attack.hig, target[i].x, target[i].y, target[i].wid, enemies[i].hig) == true
	  		then
	  			self.dealDamage(target[i],self.attack.meledamage)
	  			target[i]:getDamage(self.attack.meleDamage,self)
	  			print("delete "..i.." enemy")
	  			table.remove(enemies, i)
	  			collectgarbage()
	  			print(enemies[i])
	  			
	  		end
	  	end	
	  end
  
end

dofile('character.lua')